package com.quanyan.user.service.admin.impl;

import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.user.cache.MyRedisTemplate;
import com.quanyan.user.mapper.*;
import com.quanyan.user.model.db.*;
import com.quanyan.user.model.vo.request.admin.ReqUserQuery;
import com.quanyan.user.model.vo.response.admin.RespUser;
import com.quanyan.user.model.vo.response.admin.RespUserGroup;
import com.quanyan.user.service.admin.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by hua on 2016/8/12.
 */
@Service
public class UserServiceImpl implements UserService {
    private final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @Autowired
    private UserAdminMapper userAdminMapper;

    @Autowired
    private TbProvinceMapper provinceMapper;

    @Autowired
    private TbAdminGroupMapper adminGroupMapper;

    @Autowired
    private UserExtMapper userExtMapper;

    @Autowired
    private TbUserGroupMapper userGroupMapper;


    @Override
    public PageObj<List<RespUser>> queryUserList(ReqUserQuery userQuery) {
        List<RespUser> users = null;
        int pageNo = userQuery.getPageNo();
        int total = 0;
        try {
            userQuery.setPageNo((userQuery.getPageNo()) * userQuery.getPageSize());
            if (userQuery.getStartTime() != null) {
                userQuery.setStartTime(DateUtils.dayBegin(userQuery.getStartTime()));
            }
            if (userQuery.getEndTime() != null) {
                userQuery.setEndTime(DateUtils.dayEnd(userQuery.getEndTime()));
            }
            users = userAdminMapper.getUserList(userQuery);
            List<Integer> uids = new ArrayList<>();
            Map<Integer,RespUser> respUserMap = new HashMap<>();
            for (RespUser respUser : users) {
                respUserMap.put(respUser.getId(),respUser);
                uids.add(respUser.getId());
            }

            List<RespUserGroup> userGroups = userAdminMapper.getUserGroup(uids);
            if(!userGroups.isEmpty() && userGroups.size()> 0 ){
                for (RespUserGroup userGroup : userGroups) {
                    RespUser respUser = respUserMap.get(userGroup.getUid());
                    respUser.setGroupId(userGroup.getGroupId());
                    respUser.setGroupName(userGroup.getGroupName());
                    respUserMap.put(userGroup.getUid(),respUser);
                }
            }
            users.clear();
            for (Map.Entry<Integer,RespUser> entry : respUserMap.entrySet()){
                users.add(entry.getValue());
            }

            Collections.sort(users);
            total = userAdminMapper.countByQuery(userQuery);

        } catch (Exception e) {
            logger.error("获取用户列表失败！");
        }
        return PageObj.create(total, pageNo, userQuery.getPageSize(), users);
    }

    @Override
    public RespUser queryUserDetail(Integer uid) {
        RespUser respUser = null;
        try {
            respUser = userAdminMapper.queryUserDetail(uid);
            if (respUser != null) {
                TbUserExt tbUserExt = userExtMapper.queryUserExtByUid(uid);
                if (tbUserExt != null) {
                    respUser.setBgUrl(tbUserExt.getBgUrl());
                    respUser.setBmi(tbUserExt.getBmi());
                    respUser.setHeight(tbUserExt.getHeight());
                    respUser.setWeight(tbUserExt.getWeight());
                    respUser.setCompany(tbUserExt.getCompany());
                    respUser.setNameVerifyStatus(tbUserExt.getNameVerifyStatus());
                    respUser.setPhysique(tbUserExt.getPhysique());
                    respUser.setBgUrl(tbUserExt.getBgUrl());
                    respUser.setZodiac(tbUserExt.getZodiac());
                    respUser.setProfession(tbUserExt.getProfession());
                    respUser.setIndustry(tbUserExt.getIndustry());
                    respUser.setSecondIndustry(tbUserExt.getSecondIndustry());
                    respUser.setIsBidsVerify(tbUserExt.getIsBidsVerify());
                    respUser.setIsBuyoutVerify(tbUserExt.getIsBuyoutVerify());
                    respUser.setUserInterests(userAdminMapper.queryUserInterests(uid));
                }
            }
        } catch (Exception e) {
            logger.error("获取用户详细信息失败！");
        }
        return respUser;
    }

    @Override
    public List<TbProvince> getProvinceList() {
        List<TbProvince> provinces = null;
        try {
            provinces = provinceMapper.selectByExample(null);
        } catch (Exception e) {
            logger.error("获取用户列表失败！");
        }
        return provinces;
    }

    @Override
    public List<TbCity> getCitys(Integer provinceId) {
        List<TbCity> cities = null;
        try {
            cities = userAdminMapper.queryTbCityList(provinceId);
        } catch (Exception e) {
            logger.error("获取用户列表失败！");
        }
        return cities;
    }

    @Override
    public void updateUserGroup(int uid, int groupId) {
        try {
            userAdminMapper.updateUserGroup(uid, groupId);
        } catch (Exception e) {
            logger.error("更新用户分组标签失败！");
        }
    }

    @Override
    public List<TbAdminGroup> getAdminGroups() {
        try {
            return adminGroupMapper.selectByExample(new TbAdminGroupExample());
        } catch (Exception e) {
            logger.error("获取用户分组标签列表失败！");
            return null;
        }
    }

    @Override
    public TbAdminGroup insertGroup(TbAdminGroup tbAdminGroup) {
        try {
            int ret = adminGroupMapper.insertSelective(tbAdminGroup);
            if (ret == 1) {
                return tbAdminGroup;
            }
        } catch (Exception e) {
            logger.error("添加用户分组标签失败！");
        }
        return null;
    }

    @Override
    public int deleteGroup(int id) {
        try {
            return adminGroupMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            logger.error("删除用户分组标签失败！");
        }
        return 0;
    }

    /**
     * 测试之用
     * @param uid
     * @param friendId
     * @return
     */
    @Override
    public int insertTest(Integer uid, Integer friendId) {
        TbUserGroup group = new TbUserGroup();
        group.setUid(uid);
        group.setGid(friendId);
        int n = userGroupMapper.insertSelective(group);
        return n;
    }


}
